const zbEditTable = function () {
    "use strict";

    //根据传入的表头创建空值的可编辑表格
    //xTilte, yTitle 是以逗号隔开的字符串
    this.createTitledEditTable = function (xTitle, yTitle, config={}) {

        let xitem = xTitle.replace(/，/g, ',');
        let arrXitem = xitem.split(',');
        let yitem = yTitle.replace(/，/g, ',');
        let arrYitem = yitem.split(',');

        let table = document.createElement('table');
        if (config['clsssTable'] !== undefined) {
            table.classList.add(config['clsssTable']);
        }
        if (config['idTable'] !== undefined) {
            table.setAttribute('id', config['idTable']);
        }

        let tr = document.createElement('tr');
        if (config['clsssTr'] !== undefined) {
            tr.classList.add(config['clsssTr']);
        }

        let trCopy = tr.cloneNode();
        let name = '0_';
        for (let x=0; x<arrXitem.length; x++) {
            trCopy.appendChild(this.createEditCell(name+''+x, arrXitem[x], config));
        }
        table.appendChild(trCopy);

        let lineNum =1;
        for (let y=0; y<arrYitem.length; y++) {
            trCopy = tr.cloneNode();
            let index= y + lineNum;
            name = index+'_';
            //每一行的第一个td
            trCopy.appendChild(this.createEditCell(name+'0', arrYitem[y], config));

            //每一行的其余td
            for (let x=0; x<arrXitem.length - 1; x++) {
                let j = x+1;
                trCopy.appendChild(this.createEditCell(name+''+j, '', config));
            }

            table.appendChild(trCopy);
        }

        return table; //返回DOM元素, target.appendChild(table);
    }

    //根据传入的行数,列数创建空的可编辑的表格
    this.createEmptyEditTable = function (rowNum, colNum, config={}) {

        let table = document.createElement('table');
        if (config['clsssTable'] !== undefined) {
            table.classList.add(config['clsssTable']);
        }

        let tr = document.createElement('tr');
        if (config['clsssTr'] !== undefined) {
            tr.classList.add(config['clsssTr']);
        }

        for (let y=0; y<rowNum; y++) {
            let trCopy = tr.cloneNode();

            //每一行的其余td
            for (let x=0; x<colNum; x++) {
                let name = y+'_'+x;
                trCopy.appendChild(this.createEditCell(name, '', config));
            }

            table.appendChild(trCopy);
        }

        return table; //返回DOM元素, target.appendChild(table);
    }

    //根据传入的数据,创建一个可编辑的表格
    this.createValuedEditTable = function (arr, config={}) {

        let table = document.createElement('table');
        if (config['clsssTable'] !== undefined) {
            table.classList.add(config['clsssTable']);
        }

        let tr = document.createElement('tr');
        if (config['clsssTr'] !== undefined) {
            tr.classList.add(config['clsssTr']);
        }

        for (let y=0; y<arr.length; y++) {
            let trCopy = tr.cloneNode();
            for (let x in arr[y]) {
                trCopy.appendChild(this.createEditCell(x, arr[y][x], config));
            }

            table.appendChild(trCopy);
        }

        return table; //返回DOM元素, target.appendChild(table);
    }

    //根据传入的数据,创建一个只读的表格
    this.createValuedReadTable = function (arr, config={}) {

        let table = document.createElement('table');
        if (config['clsssTable'] !== undefined) {
            table.classList.add(config['clsssTable']);
        }

        let tr = document.createElement('tr');
        if (config['clsssTr'] !== undefined) {
            tr.classList.add(config['clsssTr']);
        }

        for (let y=0; y<arr.length; y++) {
            let trCopy = tr.cloneNode();

            //每一行的其余td
            for (let x in arr[y]) {
                trCopy.appendChild(this.createReadCell(x, arr[y][x], config));
            }

            table.appendChild(trCopy);
        }

        return table; //返回DOM元素, target.appendChild(table);
    }

    //创建可以编辑的表格td
    this.createEditCell = function (name, value, config={}) {
        let td = document.createElement('td');
        if (config['classTd'] !== undefined) {
            td.classList.add(config['classTd']);
        }

        let input = document.createElement('input');
        input.setAttribute('type', 'text');
        //input.setAttribute('id', name);
        input.setAttribute('name', name);
        input.setAttribute('value', value);
        if (config['classInput'] !== undefined) {
            input.classList.add(config['classInput']);
        }

        td.appendChild(input);
        return td;
    }

    //创建只读的表格td
    this.createReadCell = function (name, value, config={}) {
        let td = document.createElement('td');
        if (config['classTd'] !== undefined) {
            td.classList.add(config['classTd']);
        }

        td.innerText = value;
        return td;
    }

    //将字符串数据转为数组
    this.convertStrToArr = function (str, trGap=';', tdGap='&', kvGap = '=') {
        let arrTr = str.split(trGap); //拆分成多行
        let data = [];

        for (let i=0; i<arrTr.length; i++) {
            let arrTd = arrTr[i].split(tdGap); //拆分成td
            let obj = {};
            for (let j=0; j<arrTd.length; j++) {
                let arr = arrTd[j].split(kvGap);
                obj[arr[0]] = arr[1];
            }
            data.push(JSON.parse(JSON.stringify(obj)));
        }

        return data;
    }

    this.getInputs = function (id, trGap=';', tdGap='&', kvGap = '=') {
        let trs = document.getElementById(id).getElementsByTagName('tr');
        let data = [];
        for (let i = 0; i < trs.length; i++) {
            let rowData = [];
            let inputs = trs[i].getElementsByTagName('input');
            for (let j = 0; j < inputs.length; j++) {
                let name = inputs[j].name;
                let value = inputs[j].value;
                let str = name + kvGap + value;
                rowData.push(str);
            }
            data.push(rowData.join(tdGap));
        }

        return data.join(trGap);
    }

    this.domToHtml = function (node) {
        let tmpNode = document.createElement( "div" );
        tmpNode.appendChild(node);
        return tmpNode.innerHTML;
        //tmpNode = node = null; // prevent memory leaks in IE
    }

    //获取表格的值
    // str: 通过getInputs获取的字符串值; row 行数(从0开始); col 列数(从0开始)
    this.getValue = function (str, row=0, col=0) {
        let arr = this.convertStrToArr(str);
        let line = arr[row];
        let key = row + '_' + col
        return line[key];
    }
}